package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.wx.AddToCartBO;
import com.cskaoyan.bean.bo.wx.FastAddBO;
import com.cskaoyan.bean.bo.wx.UpdateCheckedBO;
import com.cskaoyan.bean.bo.wx.UpdateNumInCartBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.wx.CartCheckoutVO;
import com.cskaoyan.bean.vo.wx.CartIndexVo;
import com.cskaoyan.service.wx.CartService;
import com.cskaoyan.utils.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author klw
 * @Data 2022/11/22 下午10:01
 */


@RestController
@RequestMapping("wx/cart")
public class CartController {

    @Autowired
    CartService cartService;



    @GetMapping("goodscount")
    public BaseRespVo getGoodsCountInCart(){

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.ok(0);
        }
        Integer userId =  user.getId();
        //Integer userId = 1;

//        if(null == userId){
//            return BaseRespVo.ok(0);
//        }

        int count = cartService.getGoodsCountInCart(userId);

        return BaseRespVo.ok(count);
    }


    @PostMapping("add")
    public BaseRespVo addGoodsToCart(@RequestBody AddToCartBO addToCartBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;

        if(null == userId){
            return BaseRespVo.error(501,"请登录");
        }

        int res = cartService.addGoods(userId,addToCartBO.getGoodsId(),addToCartBO.getNumber(),addToCartBO.getProductId());
        return BaseRespVo.ok(res);
    }

    @GetMapping("index")
    public BaseRespVo indexOfCart(){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;
        CartIndexVo cartIndexVo = cartService.getIndex(userId);
        return BaseRespVo.ok(cartIndexVo);
    }

    @PostMapping("checked")
    public BaseRespVo changeChecked(@RequestBody UpdateCheckedBO updateCheckedBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;
        CartIndexVo cartIndexVo = cartService.updateChecked(userId,updateCheckedBO);
        return BaseRespVo.ok(cartIndexVo);
    }


    @PostMapping("update")
    public BaseRespVo updateNumInCart(@RequestBody UpdateNumInCartBO updateNumInCartBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;
        cartService.updateNum(userId,updateNumInCartBO);
        return BaseRespVo.ok();
    }


    @PostMapping("delete")
    public BaseRespVo deleted(@RequestBody UpdateCheckedBO updateCheckedBO) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;
        CartIndexVo cartIndexVo = cartService.updatedeleted(userId, updateCheckedBO);
        return BaseRespVo.ok(cartIndexVo);
    }


    @PostMapping("fastadd") //返回购物车id
    public BaseRespVo fastAddCart(@RequestBody FastAddBO fastAddBO){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;
        int newCartId = cartService.fastAddCart(userId,fastAddBO);
        return BaseRespVo.ok(newCartId);
    }


    @GetMapping("checkout") //checkout?cartId=128&addressId=0&couponId=0&userCouponId=0&grouponRulesId=0
    public BaseRespVo checkout(Integer cartId,Integer addressId, Integer couponId,Integer userCouponId,Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        if(null == user){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId =  user.getId();
        //Integer userId = 1;

        CartCheckoutVO cartCheckoutVO = cartService.getCartCheckVO(cartId,addressId,couponId,userCouponId,grouponRulesId,userId);
        return BaseRespVo.ok(cartCheckoutVO);
    }

}
